﻿using System;
using System.IO;
using Microsoft.Kinect;
using System.Runtime.Serialization.Formatters.Binary;
using DetectingKinectSencor;

/// <summary>
/// The following class was provided by the book 
/// Microsoft.Press.Programming.with.the.Kinect.for.Windows.Software.Development.Kit.Sep.2012
/// </summary>
public class PlaySkeletonStream : FramePlayer
{
    private bool DEBUGon = MainWindow.DEBUGplaySkeltonStream;
    bool DEBUGstreamWriterReplayOn = MainWindow.DEBUGstreamWriterReplay;

    public Tuple<float, float, float, float> FloorClipPlane { get; private set; }
    public Skeleton[] Skeletons { get; private set; }


    public PlaySkeletonStream(SkeletonFrame frame)
    {
        FloorClipPlane = frame.FloorClipPlane;
        frameNumber = frame.FrameNumber;
        timeStamp = frame.Timestamp;

        Skeletons = frame.GetSkeletons();
    }

    public PlaySkeletonStream()
    {
    }

    internal override void CreateFromReader(BinaryReader reader)
    {
        type = "skeleton";
        timeStamp = reader.ReadInt64();
        FloorClipPlane = new Tuple<float, float, float, float>(
                                                                reader.ReadSingle(), reader.ReadSingle(),
                                                                reader.ReadSingle(), reader.ReadSingle());
        frameNumber = reader.ReadInt32();
        BinaryFormatter formatter = new BinaryFormatter();

        Skeletons = (Skeleton[])formatter.Deserialize(reader.BaseStream);
    }

    public static implicit operator PlaySkeletonStream(SkeletonFrame frame)
    {
        return new PlaySkeletonStream(frame);
    }
}